Helpful Information
 
 
Category: Converting C
Converting C -> C#

I am converting one of my C programs to C#. However, there are a couple of functions which I do not know the C#'ed version of. The first one is malloc, I use to to create an array (int *blocks = (int *) malloc(size * sizeof(int)); however I am not sure of the best way to do this in C#. I also do not know where the random number generator is (e.g. srand and rand). Can someone help me?

You don't need malloc anymore!! You create arrays in C# this way:

string[] s = new string[10];

Or if you need dynamic arrays (i.e. arrays that can grow or shrink dynamically), you can use the ArrayList class:

System.Collections.ArrayList al = new System.Collections.ArrayList();

To create random numbers in C#, use the Random class in the System.Random namespace . . .


I am converting one of my C programs to C#. However, there are a couple of functions which I do not know the C#'ed version of. The first one is malloc, I use to to create an array (int *blocks = (int *) malloc(size * sizeof(int)); however I am not sure of the best way to do this in C#. I also do not know where the random number generator is (e.g. srand and rand). Can someone help me?

The most important thing about converting from C to C#, aside from the whole object-oriented thingy, is that stuff created on the heap, such as your dynamic array with malloc (and about 99% of everything you'll do in C#), needs not be deleted with free. Managed memory with garbage collection leaves you free to worry about other things.

The most important thing about converting from C to C#, aside from the whole object-oriented thingy, is that stuff created on the heap, such as your dynamic array with malloc (and about 99% of everything you'll do in C#), needs not be deleted with free. Managed memory with garbage collection leaves you free to worry about other things. yes, that is one thing that C# does nicely. Especially coupled with Managed DirectX (if you do 3d programming).










privacy (GDPR)